package com.warrior.superdata.dds;

import com.warrior.superdata.dds.datasource.DBIdentifier;
import com.warrior.superdata.dds.datasource.DynamicDataSource;
import com.warrior.superdata.dds.datasource.ProjectDBMgr;
import com.warrior.superdata.dds.domain.DBVo;
import com.warrior.superdata.dds.esclient.ElasticsearchRestClientHolder;
import com.warrior.superdata.enums.DatasourceType;
import com.warrior.superdata.mysql.entity.dto.ApiDatasourceDto;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.Statement;

/**
 * @Description:
 * @ClassName: com.warrior.superdata.dds.datasource.DataSourceHelp.java
 * @date: 2022/3/2 18:25
 * @author xujian
 */
@Component
@Slf4j
public class DataSourceHelp {

    private volatile DynamicDataSource dynamicDataSource = null;

    public DynamicDataSource getDynamicDataSource() {
        if (null == dynamicDataSource) {
            synchronized (DynamicDataSource.class) {
                if (null == dynamicDataSource) {
                    dynamicDataSource = new DynamicDataSource();
                }
            }
        }
        return dynamicDataSource;
    }

    public Connection getConnect(ApiDatasourceDto datasourceDto) {
        String ip = datasourceDto.getIp();
        String port = datasourceDto.getPort();
        String dataSoureTypeName = DatasourceType.getNameByType(datasourceDto.getType());
        String projectCode = ip + "," + port + dataSoureTypeName + datasourceDto.getUsername() + datasourceDto.getPassword();
        //如果数据源信息map里面没有该数据源的信息需要加入数据源信息map中
        if (null == ProjectDBMgr.instance().getDBInfo(projectCode)) {
            DBVo dbVo = new DBVo();
            dbVo.setIp(datasourceDto.getIp());
            dbVo.setPort(datasourceDto.getPort());
            dbVo.setUsername(datasourceDto.getUsername());
            dbVo.setPassword(datasourceDto.getPassword());
            ProjectDBMgr.instance().addDBInfo(projectCode, dbVo);
        }
        //以数据库ip为标识放入ThreadLocal中
        DBIdentifier.setProjectCode(projectCode);
        return getDynamicDataSource().getConnection();
    }

    public RestHighLevelClient getRestHighLevelClient(ApiDatasourceDto datasourceDto) {
        return getRestHighLevelClient(datasourceDto, Boolean.TRUE);
    }

    public RestHighLevelClient getRestHighLevelClient(ApiDatasourceDto datasourceDto, Boolean isCache) {
        DBVo dbVo = new DBVo();
        dbVo.setIp(datasourceDto.getIp());
        dbVo.setPort(datasourceDto.getPort());
        dbVo.setUsername(datasourceDto.getUsername());
        dbVo.setPassword(datasourceDto.getPassword());
        return ElasticsearchRestClientHolder.getInstance().getRestHighLevelClient(dbVo, isCache);
    }

    public void close(Statement statement, Connection connection) {
        try {
            if (null != statement) {
                statement.close();
            }
            if (null != connection) {
                connection.close();
            }
        } catch (Exception e) {
            log.error("关闭流异常 errMsg:{}", e.getMessage());
        }
    }

}
